<?php

namespace addons\TfFutures\api\controllers;

use addons\TfFutures\common\models\Bankinfo;
use addons\TfFutures\common\models\Identity;
use addons\TfFutures\services\OrderService;
use addons\TfFutures\common\models\Logs;
use common\helpers\ResultDataHelper;
use common\models\common\PayLog;
use Yii;
use api\controllers\OnAuthController;
use addons\TfFutures\common\models\Member;

class MemberController extends OnAuthController
{
    public $modelClass = Member::class;

//    protected $optional = ['my-info'];

    //当前用户信息
    public function actionMyInfo()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api('422', 'Access done!');
        }

        $member_id = Yii::$app->user->identity->member_id;
        $arr = Member::Detail($member_id);
        return ResultDataHelper::api('200', 'success',$arr);
    }

    /**
     * 用户流水查询
     *
     * */

    public function actionDetail()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api('403', 'Access done!');
        }
        //账户id
        $id = Yii::$app->user->identity->member_id;
        if (empty($id)) {
            return ResultDataHelper::api('422', '用户id不能为空');
        }
        $logs = Logs::find()
            ->Where(['member_id' => $id])
            ->orderBy('created_at desc')
            ->all();
        return ResultDataHelper::api('200', 'success', $logs);
    }
    /**
     * 提交用户实名认证
     *
     */
    public function actionUpload(){
        if(!Yii::$app->request->isPost){
            return ResultDataHelper::api('403', 'access done');
        }
        $uid = Yii::$app->user->identity->member_id;
        $model = new Identity();
        $models = new Bankinfo();
        $data = Yii::$app->request->post();

        if(empty($data['code'])){
            return ResultDataHelper::api('422','请输入身份证');
        }
        if(empty($data['bank_code'])){
            return ResultDataHelper::api('422','请输入银行卡号');
        }
        $identity = Identity::find()->where(['member_id' => $uid,])->andWhere(['<>', 'status', '0'])->one();
        if (!empty($identity)) {
            return ResultDataHelper::api(200, '用户已经提交验证信息');
        }
        //身份证提交
        $model->setAttributes([
            'member_id'    => $uid,
            'code' => $data['code'],
            'real_name' => $data['real_name'],
            'created_at' => time(),
            'updated_at' => time(),
            'status'     => '1',
        ]);
        //银行卡提交
        $models->setAttributes([
            'bank_name' =>$data['bank_name'],
            'bank_code' =>$data['bank_code'],
            'rale_name' =>$data['real_name'],
            'child_name' =>$data['child_name'],
            'member_id' =>$uid,
            'created_at' => time(),
            'updated_at' => time(),
            'status'     => '1',
        ]);

        if($model->save() && $models->save()){
            return ResultDataHelper::api('200', 'success');
        }else{
            return ResultDataHelper::api('500', $model->getFirstErrors());
        }

    }
    /**
     * 获取身份验证银行卡信息 GET
     * @return mixed
     */
    public function actionIdentityStatus()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(403, 'access done');
        }
        //获取id
        $user_id = Yii::$app->user->identity->member_id;
        if (is_null($user_id)) {
            return ResultDataHelper::api(422, '用户名为空');
        }
        $model = Identity::find()
            ->orderBy('id desc')

            ->where(['member_id' => $user_id])
            ->one();
        if (empty($model)) {
            return ResultDataHelper::api(422, '该用户没有提交身份验证数据', $model->errors);
        }
        $models = Bankinfo::find()
            ->where(['member_id'=>$user_id])
            ->one();
        $data['real_name'] = $model->real_name;
        $data['code'] = $model->code;
        $data['bank_name'] = $models->bank_name;
        $data['bank_code'] = $models->bank_code;
        $data['child_name'] = $models->child_name;
        $model->created_at = date('Y-m-d H:i:s', $model->created_at);
        $model->updated_at = date('Y-m-d H:i:s', $model->updated_at);
        $data['status'] = $model->status=='0'?'未通过':($model->status=='1'?'待审核':'通过');
        return ResultDataHelper::api(200, 'success', $data);
    }

}